import java.util.HashMap;
import java.util.Map;

public class MinNumberOfFrogs {
    //数青蛙
    public int minNumberOfFrogs(String c) {
        char[] croakOfFrogs = c.toCharArray();

        //说明叫声
        String t = "croak";
        int len = t.length();
        int[] hash = new int[len];

        //录入下标
        Map<Character, Integer> indexs = new HashMap<>(); //下标的对应
        for (int i = 0; i < len; i++){
            indexs.put(t.charAt(i), i);
        }

        for (char ch : croakOfFrogs){
            if (ch == t.charAt(0)){ //遇到c的情况
                if (hash[len - 1] != 0){
                    hash[len - 1]--;
                }

                hash[0]++;
            }else{
                int index = indexs.get(ch);
                if (hash[index - 1] == 0){
                    return -1;
                }

                hash[index - 1]--;
                hash[index]++;
            }
        }

        for (int i = 0; i < len - 1; i++){
            if (hash[i] != 0){
                return -1;
            }
        }

        return hash[len - 1];
    }
}
